import 'package:flutter/material.dart';
import '../services/screenAdapter.dart';
import '../modules/home/controllers/home_controller.dart';
import 'package:get/get.dart';

import 'package:dropdown_button2/dropdown_button2.dart';

class CreateProject extends StatelessWidget {
  CreateProject({super.key});

  final HomeController controller = Get.find();
  Widget _buildDropdown() {
    return DropdownButtonHideUnderline(
      child: DropdownButton2<int>(
        isExpanded: true,
        hint: Text('选择分类'),
        value: controller.selectedValue.value,
        items: controller.leftGroupList.map((item) {
          final isSelected = item?.id == controller.selectedValue.value;
          return DropdownMenuItem<int>(
            value: item?.id,
            child: Container(
              width: double.infinity,
              padding: const EdgeInsets.symmetric(vertical: 8),
              decoration: BoxDecoration(borderRadius: BorderRadius.circular(4)),
              child: Text(
                item?.groupName ?? '',
                style: TextStyle(
                  fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                ),
              ),
            ),
          );
        }).toList(),
        onChanged: controller.onChanged,
        buttonStyleData: ButtonStyleData(
          height: ScreenAdapter.height(50),
          width: double.infinity,
          padding: EdgeInsets.symmetric(horizontal: 16),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            color: Colors.white,
          ),
          elevation: 0,
        ),
        menuItemStyleData: MenuItemStyleData(height: ScreenAdapter.height(50)),
        dropdownStyleData: DropdownStyleData(
          maxHeight: ScreenAdapter.height(200),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            color: Colors.white,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Material(
        type: MaterialType.transparency,
        child: Container(
          width: Get.width * 0.5,
          padding: EdgeInsets.all(ScreenAdapter.width(24)),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(16),
            boxShadow: [
              BoxShadow(color: Colors.black26, blurRadius: 12, spreadRadius: 2),
            ],
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '添加新项目',
                style: TextStyle(
                  fontSize: ScreenAdapter.fontSize(24),
                  fontWeight: FontWeight.bold,
                  color: Colors.blue[800],
                ),
              ),
              SizedBox(height: ScreenAdapter.height(24)),

              // 项目名称输入
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("项目名称", style: TextStyle(fontWeight: FontWeight.w500)),
                  SizedBox(height: ScreenAdapter.height(8)),
                  TextFormField(
                    onChanged: (value) {
                      controller.createProjectName.value = value;
                    },
                    decoration: InputDecoration(
                      hintText: "请输入项目名称",
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(8),
                        borderSide: BorderSide(color: Colors.grey[300]!),
                      ),
                      contentPadding: EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 14,
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(height: ScreenAdapter.height(16)),
              Obx(
                () => Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text("所属分组", style: TextStyle(fontWeight: FontWeight.w500)),
                    SizedBox(height: ScreenAdapter.height(8)),
                    Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8),
                        border: Border.all(
                          color: Colors.grey,
                        ),
                      ),
                      child: _buildDropdown(),
                    ),
                  ],
                ),
              ),

              SizedBox(height: ScreenAdapter.height(24)),
              // 操作按钮
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  TextButton(
                    onPressed: () => Get.back(),
                    child: Text(
                      '取消',
                      style: TextStyle(color: Colors.grey[700]),
                    ),
                  ),
                  SizedBox(width: ScreenAdapter.width(16)),
                  ElevatedButton(
                    onPressed: () {
                      controller.submitProjectForm();
                    },
                    style: ElevatedButton.styleFrom(
                      padding: EdgeInsets.symmetric(
                        horizontal: 24,
                        vertical: 12,
                      ),
                    ),
                    child: Text('确认添加'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
